##Script for near distance classifaction and counting for feature polygons.
##Written by John Talbot

import datetime
from datetime import datetime
#import tools
import arcpy
import csv
from arcpy import env
env.workspace="c:\temp"
arcpy.env.overwriteOutput = 'True'

#Here we are going to delcare some global arrays. These arrays will be used
#later as a kind of virtual table in memory to keep track of out data.
#a 2-d array could also be used instead, and may actually be more appropriate

global polyarray,p25,p50,p100,p150,p200,p250,p300,p350,p400,p450,p500
polyarray=[]
p25=[]
p50=[]
p100=[]
p150=[]
p200=[]
p250=[]
p300=[]
p350=[]
p400=[]
p450=[]
p500=[]

#set data variables
dbase="C:\\temp\\Year_Lines_Proj.gdb\\"
polys="gadm2_w_possible_mangrove"

stormlist=["a1998","a1999","a2000","a2001","a2002","a2003","a2004","a2005","a2006","a2007","a2008","a2009","a2010","a2011","a2012","a2013"]

storm="_TS_1_DissExd"

dist="500"

#define main block.
def main():
    #Here the arrays we declared earlier will be set to contain 11227 integers. I have 11227 polygons, so I need 11227 data positions
    #in each array. As long as the arrays are not sorted or have data positions appended or deleted, position 1 in one array corresponds to
    #postion 1 in all the others. This is called parralel arrays and I am going to use them to store our counts. Because it is all in memory,
    #it should run quickly.
    print "Initializing arrays ", datetime.now()
    

    #Here is the mainloop. for each year file, a near table in generated. It takes about an hour to run on just one years worth of storm lines.
    for i in stormlist:
        
        for z in range(1,11228):
            polyarray.append(z)
            p25.append(0)
            p50.append(0)
            p100.append(0)
            p150.append(0)
            p200.append(0)
            p250.append(0)
            p300.append(0)
            p350.append(0)
            p400.append(0)
            p450.append(0)
            p500.append(0)
        
        stormline=dbase+i+storm
        print "Creating stormline realtion for ", stormline, datetime.now()
        arcpy.GenerateNearTable_analysis(dbase+polys,stormline,dbase+i,dist+" KILOMETERS","","","All")
        print "Counting hits in ", stormline, datetime.now()
        print i
        hitCounter(dbase+i)
        csvWiz(i)
        cursorWiz(dbase+polys,i+"_")
    
def hitCounter(table):
    #here we cursor through the near table, and for each fid, we find the position in the main array (polyarray) that corresponds to the
    #FID. Remeber, this postion is true throughout all the arrays. Then we run the NEAR_DIST through an elif series to determine the appopritae arrays
    #to increment. 
    
    fidList=[]
    cursor= arcpy.UpdateCursor(table)
    for i in cursor:
        pos=polyarray.index(i.IN_FID)
        x=i.NEAR_DIST
        
        if x > 450000:
            
            p500[pos]=p500[pos]+1
        elif x > 400000:
            
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
        elif x > 350000:
            
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 300000:
            
            p350[pos]=p350[pos]+1 
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 250000:
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1 
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 200000:
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1 
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 150000:
            p200[pos]= p200[pos]+1
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1 
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 100000:
            p150[pos]= p150[pos]+1
            p200[pos]= p200[pos]+1
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
            
        elif x > 50000:
           
            p100[pos]= p100[pos]+1
            p150[pos]= p150[pos]+1
            p200[pos]= p200[pos]+1
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
        elif x > 25000:
            p50[pos]= p50[pos]+1
            p100[pos]= p100[pos]+1
            p150[pos]= p150[pos]+1
            p200[pos]= p200[pos]+1
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]=p350[pos]+1
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1
        else:
            p25[pos]=p25[pos]+1
            p50[pos]= p50[pos]+1
            p100[pos]= p100[pos]+1
            p150[pos]= p150[pos]+1
            p200[pos]= p200[pos]+1
            p250[pos]= p250[pos]+1
            p300[pos]= p300[pos]+1
            p350[pos]= p350[pos]+1
            p400[pos]= p400[pos]+1
            p450[pos]= p450[pos]+1
            p500[pos]= p500[pos]+1

#This guy below just writes out the final results of the arrays.        
def csvWiz(name):
    
    writer = open("C:\\temp\\"+name+".csv",'w')
    writer.write(str(name)+"\n FID_IN, 25k, 50k, 100k, 150k, 200k, 250k, 300k, 350k, 400k, 450k, 500k \n")
    for i in range(0,11227):
        writer.write(str(polyarray[i])+","+str(p25[i])+","+str(p50[i])+","+str(p100[i])+","+str(p150[i])+","+str(p200[i])+","+str(p250[i])+","+str(p300[i])+","+str(p350[i])+","+str(p400[i])+","+str(p450[i])+","+str(p500[i])+"\n")
    
def cursorWiz(polys,name):
    distlist=["25","50","100","150","200","250","300","350","400","450","500"]
    arrlist=[p25,p50,p100,p150,p200,p250,p300,p350,p400,p450,p500]
    c=0
    for i in distlist:
        newf=name+i
        f=[newf,"OBJECTID"]
        arcpy.AddField_management(polys,newf,"SHORT")
        cursor=arcpy.da.UpdateCursor(polys,f)
        arr=arrlist[c]
        for q in cursor:
            q[0] = arr[(q[1]-1)]
            cursor.updateRow(q)
        c=c+1
            
        
        

    
        
    

main()

    
